package com.easy.cloud.web.module.dict.biz.controller;

import com.easy.cloud.web.component.core.response.HttpResult;
import com.easy.cloud.web.module.dict.api.dto.DictDTO;
import com.easy.cloud.web.module.dict.api.dto.DictQueryDTO;
import com.easy.cloud.web.module.dict.api.vo.DictVO;
import com.easy.cloud.web.module.dict.biz.service.IDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * Dict API
 *
 * @author Fast Java
 * @date 2023-08-04 14:22:33
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping(value = "dict")
@Api(value = "字典管理", tags = "字典管理")
public class DictController {

    private final IDictService dictService;

    /**
     * 新增
     *
     * @param dictDTO 新增数据
     * @return 新增数据
     */
    @PostMapping(value = "save")
    @PreAuthorize("@pms.hasPermission('system:dict:add')")
    @ApiOperation(value = "新增")
    public HttpResult<DictVO> save(@Validated @RequestBody DictDTO dictDTO) {
        return HttpResult.ok(dictService.save(dictDTO));
    }

    /**
     * 更新
     *
     * @param dictDTO 新增数据
     * @return 更新数据
     */
    @PostMapping(value = "update")
    @PreAuthorize("@pms.hasPermission('system:dict:update')")
    @ApiOperation(value = "更新")
    public HttpResult<DictVO> update(@Validated @RequestBody DictDTO dictDTO) {
        return HttpResult.ok(dictService.update(dictDTO));
    }

    /**
     * 根据ID移除数据
     *
     * @param dictId ID
     * @return 是否删除成功
     */
    @GetMapping(value = "remove/{dictId}")
    @PreAuthorize("@pms.hasPermission('system:dict:delete')")
    @ApiOperation(value = "根据ID移除数据")
    public HttpResult<Boolean> removeById(
            @PathVariable @NotNull(message = "当前ID不能为空") String dictId) {
        return HttpResult.ok(dictService.removeById(dictId));
    }

    /**
     * 根据ID获取详情
     *
     * @param dictId ID
     * @return 详情数据
     */
    @GetMapping(value = "detail/{dictId}")
    @PreAuthorize("@pms.hasPermission('system:dict:query')")
    @ApiOperation(value = "根据ID获取详情")
    public HttpResult<DictVO> detailById(@PathVariable @NotNull(message = "当前ID不能为空") String dictId) {
        return HttpResult.ok(dictService.detailById(dictId));
    }

    /**
     * 根据类型获取详情
     *
     * @param dictType 类型
     * @return 详情数据
     */
    @GetMapping(value = "type/{dictType}")
    @ApiOperation(value = "根据类型获取详情")
    public HttpResult<DictVO> detailByDictType(
            @PathVariable @NotNull(message = "当前字典类型不能为空") String dictType) {
        return HttpResult.ok(dictService.detailByType(dictType));
    }

    /**
     * 所有数据列表，查询参数自定义
     *
     * @param dictQueryDTO 查询参数
     * @return 查询列表
     */
    @GetMapping(value = "list")
    @PreAuthorize("@pms.hasPermission('system:dict:query')")
    @ApiOperation(value = "获取数据列表")
    public HttpResult<List<DictVO>> list(DictQueryDTO dictQueryDTO) {
        return HttpResult.ok(dictService.list(dictQueryDTO));
    }

    /**
     * 根据条件查询分页数据，查询参数自定义
     *
     * @param dictQueryDTO 查询参数
     * @return 查询分页数据
     */
    @GetMapping(value = "page")
    @PreAuthorize("@pms.hasPermission('system:dict:query')")
    @ApiOperation(value = "获取数据分页")
    public HttpResult<Page<DictVO>> page(DictQueryDTO dictQueryDTO) {
        return HttpResult.ok(dictService.page(dictQueryDTO));
    }
}